import { dialog, ipcMain, IpcMainInvokeEvent } from 'electron'
import * as query from './query'
import context from '../context'
// import fs from 'fs'

ipcMain.handle(
  'sql',
  (_event: IpcMainInvokeEvent, sql: string, type: SqlActionType, params = {}) => {
    return query[type](sql, params)
  }
)

ipcMain.handle('open', async (_event: IpcMainInvokeEvent) => {
  const res = await dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] })
  const filePath = res.canceled ? '' : res.filePaths
  // const file = fs.readFileSync(filePath[0], 'utf8')
  // return filePath[0]
  return filePath
})

ipcMain.on('openConfigWin', () => {
  context()
})
